const path = require("path");

const developPath = {
  PUB_API_BASH_URL: "'https://test-pub-api.jekunauto.com'",
  SOURCES_PATH: "'http://test-img.jekunauto.net'",
  BASE_API_URL: "'https://test-fnapp-api.jekunauto.com'",
  BASE_H5_URL: "'https://test-fn-m.jekunauto.com'"
  // BASE_H5_URL: "'http://localhost:8080'"
};
const productionPath = {
  PUB_API_BASH_URL: "'https://pub-api.jekunauto.com'",
  SOURCES_PATH: "'https://static3.jekunauto.com'",
  BASE_API_URL: "'https://fnapp-api.jekunauto.com'",
  BASE_H5_URL: "'https://fn-m.jekunauto.com'"
};

const config = {
  framework: "react",
  projectName: "FnWeixinMini-FE",
  babel: {
    plugins: [
      ['@tarojs/plugin-inject', {
        components: {
          Button: {
            bindagreeprivacyauthorization: ""
          }
        }
      }]
    ]
  },
  date: "2019-11-25",
  designWidth: 750,
  alias: {
    "@": path.resolve(__dirname, "../src"),
    "@/api": path.resolve(__dirname, "../src/api")
  },
  deviceRatio: {
    "640": 2.34 / 2,
    "750": 1,
    "828": 1.81 / 2
  },
  sourceRoot: "src",

  outputRoot: `dist/${process.env.TARO_ENV}`,
  defineConstants: {},
  copy: {
    patterns: [],
    options: {}
  },
  mini: {
    enableExtract: true,
    miniCssExtractPluginOption: {
      ignoreOrder: true
    },
    webpackChain (chain, webpack) {
      chain.merge({
        optimization: {
          splitChunks: {
            cacheGroups: {
              lodash: {
                name: "lodash",
                priority: 1000,
                test (module) {
                  return /node_modules[\\/]lodash/.test(module.context);
                }
              }
            }
          }
        }
      });
      chain.optimization.sideEffects(false);
    },
    commonChunks (commonChunks) {
      commonChunks.push("lodash");
      return commonChunks;
    },
    module: {
      postcss: {
        autoprefixer: {
          enable: true,
          config: {
            browsers: ["last 3 versions", "Android >= 4.1", "ios >= 8"]
          }
        },
        pxtransform: {
          enable: true,
          config: {}
        },
        url: {
          enable: true,
          config: {
            limit: 10240 // 设定转换尺寸上限
          }
        },
        cssModules: {
          enable: true, // 默认为 false，如需使用 css modules 功能，则设为 true
          config: {
            namingPattern: "module", // 转换模式，取值为 global/module
            generateScopedName: "[name]__[local]___[hash:base64:5]"
          }
        }
      }
    }
  },
  h5: {
    esnextModules: ["taro-ui"],
    publicPath: "/",
    staticDirectory: "static",
    module: {
      postcss: {
        autoprefixer: {
          enable: true,
          config: {
            browsers: ["last 3 versions", "Android >= 4.1", "ios >= 8"]
          }
        },
        cssModules: {
          enable: true, // 默认为 false，如需使用 css modules 功能，则设为 true
          config: {
            namingPattern: "module", // 转换模式，取值为 global/module
            generateScopedName: "[name]__[local]___[hash:base64:5]"
          }
        }
      }
    }
  }
};

module.exports = function (merge) {
  let exportConfig = {};
  if (process.env.NODE_ENV === "development") {
    exportConfig = merge({}, config, require("./dev"));
    exportConfig.defineConstants = developPath;
  } else {
    exportConfig = merge({}, config, require("./prod"));
    exportConfig.defineConstants = productionPath;
  }
  return exportConfig;
};
